package com.company.controller;


import com.company.entity.User;
import com.company.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminService adminService;

    @RequestMapping("/login")
    public void login() {
    }

    @RequestMapping("/reg")
    public void reg() {
    }

    @RequestMapping("/loginPost")
    public String loginPost(String name, String pwd, HttpSession session, Model model) {

        User u = adminService.login(name, pwd);
        if (u != null) {
            session.setAttribute("LoginUser", u);
            return "redirect:/main/index.do";
        } else {
            model.addAttribute("msg", "用户名或密码错误!");
            return "admin/login";
        }
    }

    @RequestMapping("/regPost")
    public String regPost(String name, String pwd1, String pwd2, Model model) {
        if (!pwd1.equals(pwd2)) {
            model.addAttribute("msg", "两次密码不一致");
        } else {
            int i = adminService.reg(name, pwd1);
            if (i > 0) {
                model.addAttribute("msg", "注册成功,请前往登陆!");
            } else if (i == -2) {
                model.addAttribute("msg", "用户名已存在，请重新注册!");
            } else {
                model.addAttribute("msg", "注册失败,请重新注册!");
            }
        }
        return "admin/reg";
    }
}
